﻿//using Sino.CapacityCloud.Repositories.Models;
//using Sino.CapacityCloud.WebApi;
//using Sino.Tms.Primitives;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Reflection;
//using System.Threading.Tasks;

//using TmsOrderStatus = Sino.Tms.Primitives.OrderStatus;
//using OrderState = Sino.CapacityCloud.WebApi.MyOrderState;
//using Order = Sino.CapacityCloud.WebApi.Order;
//using CcpOrder = Sino.CapacityCloud.Repositories.Models.Order;

//namespace Sino.CapacityCloud.Repositories.Utilities
//{

//    public static class ModelMappingHelper
//    {

//        public static Dictionary<string, TmsOrderStatus> OrderStateMappings;

//        static ModelMappingHelper()
//        {
//            OrderStateMappings = typeof(TmsOrderStatus).GetTypeInfo().DeclaredFields
//                .Where(x => x.IsLiteral)
//                .ToDictionary(z => z.GetCustomAttribute<EnumTextMetaAttribute>().Text, z => (TmsOrderStatus)z.GetValue(null));
//        }

//        public static OrderState ToAppOrderState(this string tmsOrderStateString)
//        {
//            var tmsOrderStatus = OrderStateMappings[tmsOrderStateString];
//            return tmsOrderStatus.ToAppOrderState();
//        }

//        public static OrderState ToAppOrderState(this TmsOrderStatus tmsOrderStatus)
//        {
//            switch (tmsOrderStatus)
//            {
//                case TmsOrderStatus.ITC:
//                case TmsOrderStatus.RTTO:
//                    return OrderState.Ungrabbed;

//                case TmsOrderStatus.HSC:
//                case TmsOrderStatus.BTS:
//                    return OrderState.Grabbed;

//                case TmsOrderStatus.WFTD:
//                    return OrderState.Waiting;

//                case TmsOrderStatus.SHIP:
//                    return OrderState.Shipping;

//                case TmsOrderStatus.TCHBD:
//                case TmsOrderStatus.OEND:
//                    return OrderState.Completed;

//                case TmsOrderStatus.None:
//                default:
//                    return OrderState.None;
//            }
//        }

//        public static TOrder ToRepositoryOrderModel<TOrder>(this CarGoCome.Model.Order order, Func<TransportLinkage, TransportLinkage> lineAdder) where TOrder : class, IOrder<TOrder>, new()
//        {
//            var line = new TransportLinkage()
//            {
//                TransportLinkageId = order.Line.Id,

//                StartingPlaceName = order.Line.StartingPlaceName,
//                StartingPlaceCode = order.Line.StartingPlaceCode,
//                StartingPlaceLocation = new Point(order.Line.StartingPlaceLatitude, order.Line.StartingPlaceLongitude),

//                DestinationPlaceName = order.Line.DestinationName,
//                DestinationPlaceCode = order.Line.DestinationCode,
//                DestinationPlaceLocation = new Point(order.Line.DestinationLatitude, order.Line.DestinationLongitude)
//            };
//            line = lineAdder(line);
//            return new TOrder()
//            {
//                Id = order.Id,
//                Amount = order.Amount,
//                AmountUnit = order.AmountUnit,
//                ArrivalTime = order.ArrivalTime,
//                CarLength = order.CarLength,
//                Category = order.Category,
//                Remarks = order.Comment,
//                ContactName = order.Contacts,
//                ContactChatId = order.ContactsChatId,
//                DeliveryTime = order.DeliveryTime,
//                GrabComment = order.GrabComment,
//                IsFocus = order.IsFocus,
//                Price = order.Price,
//                State = order.State,
//                ContactPhoneNumber = order.Telephone,
//                Type = order.Type,
//                TransportLinkageId = line.TransportLinkageId
//            };
//        }

//        public static Order ToAppOrderModel<TOrder>(this IOrder<TOrder> order) where TOrder : class, IOrder<TOrder>, new()
//        {
//            // TODO: Fix it!
//            return new Order()
//            {
//                Id = order.Id,
//                Amount = order.Amount,
//                AmountUnit = order.AmountUnit,
//                ArrivalTime = order.ArrivalTime,
//                CarLength = order.CarLength,
//                Category = order.Category,
//                Comment = order.Remarks,
//                Contacts = order.ContactName,
//                ContactsChatId = order.ContactChatId,
//                DeliveryTime = order.DeliveryTime,
//                GrabComment = order.GrabComment,
//                IsFocus = order.IsFocus,
//                Line = SystemUtils.Try(() => new Line()
//                {
//                    Id = order.TransportLinkage.TransportLinkageId,
//                    StartingPlaceName = order.TransportLinkage.StartingPlaceName,
//                    StartingPlaceCode = order.TransportLinkage.StartingPlaceCode,
//                    StartingPlaceLatitude = order.TransportLinkage.StartingPlaceLocation.Latitude,
//                    StartingPlaceLongitude = order.TransportLinkage.StartingPlaceLocation.Longitude,
//                    DestinationName = order.TransportLinkage.DestinationPlaceName,
//                    DestinationCode = order.TransportLinkage.DestinationPlaceCode,
//                    DestinationLatitude = order.TransportLinkage.DestinationPlaceLocation.Latitude,
//                    DestinationLongitude = order.TransportLinkage.DestinationPlaceLocation.Longitude
//                }),
//                Price = order.Price,
//                MyOrderState = order.State,
//                Telephone = order.ContactPhoneNumber,
//                Type = order.Type
//            };
//        }

//    }

//}